/**
 * 
 */
package org.braketester.model;

import org.braketester.entry.ExceptionResolver;
import org.extdev.device.ltr.*;
import org.extdev.libraries.ltr.LTRException;
import org.extdev.libraries.ltr.LTRAPILibraryFacade;

/**
 * @author LAP
 *
 */
public class LTRLoader {
	
	private LTRCrateConteiner conteiner;
	private LTRAPILibraryFacade ltrAPILibraryFacade;
	
	private LTRLoader() {
		ltrAPILibraryFacade = LTRAPILibraryFacade.getInstance();
		tryInicilizeLTR();
		tryOpenLTR();
		tryCreateCrateContainer();
		tryCloseLTR();
	}
	
	private void tryInicilizeLTR() {
		try {
			ltrAPILibraryFacade.initilizeLTR();
		} catch (LTRException e) {
			ExceptionResolver.getInstance().showException(e);
			System.exit(e.getExceptionNumber());
		}
	}
	
	private void tryCloseLTR() {
		try {
			ltrAPILibraryFacade.closeLTR();
		} catch (LTRException e) {
			ExceptionResolver.getInstance().showException(e);
			System.exit(e.getExceptionNumber());
		}
	}
	
	private void tryOpenLTR() {
		try {
			ltrAPILibraryFacade.openLTR();
		} catch (LTRException e) {
			ExceptionResolver.getInstance().showException(e);
			System.exit(e.getExceptionNumber());
		}
	}
	
	private void tryCreateCrateContainer() {
		try {
			conteiner = new LTRU1CrateConteiner();
		} catch (LTRException e) {
			ExceptionResolver.getInstance().showException(e);
			System.exit(e.getExceptionNumber());
		}
	}
	
	private static class PropertiesLoaderHolder {
		public static final LTRLoader instance = new LTRLoader();
	}
	
	public static LTRLoader getInstance() {
		return PropertiesLoaderHolder.instance;
	}
	
	public LTRCrateConteiner getContainer() {
		return conteiner;
	}
}
